﻿using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using Sigma.Admin.Client.Events;
using Sigma.Admin.Client.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sigma.Admin.Client.ViewModels
{
    
    public class NavigationViewModel : BindableBase, INavigationAware,IDisposable
    {
        public readonly IContainerProvider _containerProvider;
        public readonly IEventAggregator _ea;
        public readonly string _key = "IsNewInstance";

        public NavigationViewModel(IEventAggregator ea,IContainerProvider containerProvider)
        {
            _containerProvider = containerProvider;
            _ea = ea;
        }

        

        public virtual bool IsNavigationTarget(NavigationContext navigationContext)
        {
            //true是不创建新实例
            var doNotCreate = navigationContext.Parameters.ContainsKey("doNotCreate")? navigationContext.Parameters.GetValue<bool>("doNotCreate") : true;
            navigationContext.Parameters.Add(_key, !doNotCreate);
            return doNotCreate;
        }

        public virtual void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }

        public virtual void OnNavigatedTo(NavigationContext navigationContext)
        {
            
            if(!navigationContext.Parameters.ContainsKey(_key))
                navigationContext.Parameters.Add("IsNewInstance", true);
        }

        public void UpdateLoading(bool IsOpen)
        {
            _ea.UpdateLoading(new UpdateModel()
            {
                IsOpen = IsOpen
            });
        }

        public void Dispose()
        {

            _ea.SendMessage("该实例为："+this.GetHashCode());
        }
    }
}
